package com.bp.hearthealth.pulse.lnapps.ads.model

import com.bp.hearthealth.pulse.lnapps.appsflyer.AppsFlyerUtils
import com.bp.hearthealth.pulse.lnapps.base.bp_liveDataBus.BpLiveDataBus
import com.bp.hearthealth.pulse.lnapps.bp_extension.LogUtil
import com.bp.hearthealth.pulse.lnapps.bp_utils.Constants
import com.bp.hearthealth.pulse.lnapps.bp_utils.DataStoreUtil
import com.bp.hearthealth.pulse.lnapps.bp_utils.SpUtils
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch

object AdsSwitch {

    const val adOpenControl: String = "adOpenControl"//开屏广告总开关
    const val adInterControl: String = "adInterControl"//插页广告总开关
    const val adRVControl: String = "adRVControl"//激励广告总开关
    const val adNativeControl: String = "adNativeControl"//原生广告总开关
    const val adBannerControl: String = "adBannerControl"//横幅广告总开关

    const val userDailyMaxAdsClickNum: String = "userDailyMaxAdsClickNum"//用户每日最大广告点击数量
    const val userDailyMaxVideoAdsShowNum: String =
        "userDailyMaxVideoAdsShowNum"//用户每日最大全屏（开屏/插页/激励）广告展示数量
    const val adNativeLanguage: String = "adNativeLanguage"//语言选择页屏幕底部_大原生广告位
    const val adNativeGender: String = "adNativeGender"//性别选择页功能栏下方_原生广告
    const val adInterLaunchGender: String = "adInterLaunchGender"//第一次启动性别选择页后_插页广告
    const val adInterLaunchLanguage: String = "adInterLaunchLanguage"//第一次启动语言选择页后_插页广告
    const val adInterLaunchIntro: String = "adInterLaunchIntro"//第一次启动引导Guide页后_插页广告
    const val pageLaunchGender: String = "pageLaunchGender"//第一次冷启动后_性别选择页是否显示
    const val adOpenLaunch: String = "adOpenLaunch"//第一次冷启动后_开屏广告
    const val bp_rv_cd_s: String = "bp_rv_cd_s"//激励视频解锁倒计时时间
    const val adAppControl: String = "adAppControl"//所有广告开关
    const val adNativeBPResult: String = "adNativeBPResult"//血压结果页原生广告位
    const val adNativeHRReport: String = "adNativeHRReport"//心率报告页原生广告位
    const val adInterCacheControl: String = "adInterCacheControl"//是否缓存插页广告
    private val coroutineScope = CoroutineScope(Dispatchers.Main)

    fun initListener() {
        // 获取点击次数
        coroutineScope.launch {
            DataStoreUtil.readIntFlow(AdsCount.KEY_CLICK_COUNT, 0).collect { clickCount ->
                LogUtil.i("点击次数：$clickCount")
                if (clickCount >= getUserDailyMaxAdsClickNum().toInt()) {
                    // 不展示广告
                    LogUtil.i("点击次数：$clickCount 需关闭广告")
                    SpUtils.saveBoolean(adAppControl, false)
                    if (clickCount == getUserDailyMaxAdsClickNum().toInt()) {
                        BpLiveDataBus.with<Boolean>(adAppControl).postData(true)
                    }
                }
            }
        }

        // 获取展示次数
        coroutineScope.launch {
            DataStoreUtil.readIntFlow(AdsCount.KEY_DISPLAY_COUNT, 0).collect { displayCount ->
                LogUtil.i("展示次数：$displayCount")
                if (getUserDailyMaxVideoAdsShowNum().toInt() <= displayCount) {
                    if (SpUtils.getBoolean(Constants.LIMIT_30)) {
                        return@collect
                    }
                    LogUtil.i("埋点: ad_video_limit_30")
                    AppsFlyerUtils.trackEvent("ad_video_limit_30")
                    SpUtils.saveBoolean(
                        adOpenControl, false
                    )
                    SpUtils.saveBoolean(
                        adRVControl, false
                    )
                    SpUtils.saveBoolean(
                        adInterControl, false
                    )
                    SpUtils.saveBoolean(Constants.LIMIT_30, true)
                }
            }
        }
    }


    //所有广告开关
    private fun getAllAdSwitch(): Boolean {
        return SpUtils.getBoolean(adAppControl)
    }

    fun getBoolean(control: String): Boolean {
//        LogUtil.i("广告总开关：${getAllAdSwitch()}")
        if (!getAllAdSwitch()) {
            return false
        }
        return SpUtils.getBoolean(control)
    }

    fun getUserDailyMaxAdsClickNum(): String {
        val string = SpUtils.getString(userDailyMaxAdsClickNum)
        if (string.isNullOrEmpty()) {
            return "10"
        }
        return string
    }

    fun getUserDailyMaxVideoAdsShowNum(): String {
        val string = SpUtils.getString(userDailyMaxVideoAdsShowNum)
        if (string.isNullOrEmpty()) {
            return "30"
        }
        return string
//        return "5"
    }

    fun getBpRvCds(): String {
        val string = SpUtils.getString(bp_rv_cd_s)
        if (string.isNullOrEmpty()) {
            return "5"
        }
        return string
    }
}